<?php
// define('DB_SERVER', 'mysql17.000webhost.com');
// define('DB_USER', 'a4117838_simple');
// define('DB_PASSWORD', '123simple');
// define('DB_NAME', 'a4117838_simple');

define('DB_SERVER', 'localhost');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'mydatabase');

@$conn = mysql_connect (DB_SERVER, DB_USER, DB_PASSWORD);
mysql_select_db (DB_NAME,$conn);
if(!$conn){
	die( "Sorry! There seems to be a problem connecting to our database.");
}

function get_file_extension($file_name) {
	return end(explode('.',$file_name));
}

function errors($error){
	if (!empty($error))
	{
			$i = 0;
			while ($i < count($error)){
			$showError.= '<div class="msg-error">'.$error[$i].'</div>';
			$i ++;}
			return $showError;
	}// close if empty errors
} // close function


if (isset($_POST['upfile'])){
// check fields are not empty

if(get_file_extension($_FILES["uploaded"]["name"])!= 'csv')
{
    $error[] = 'Only CSV files accepted!';
}

if (!@$error){

$tot = 0;
$handle = fopen($_FILES["uploaded"]["tmp_name"], "r");

$array = explode(".",$_FILES["uploaded"]["name"]);
$tablename = $array[0];
$val = mysql_query('select 1 from '.$tablename);

if($val == FALSE)
{
    mysql_query('insert into tablelist (tablename) VALUES ("'.strtolower($tablename).'")');
    mysql_query('create table '.strtolower($tablename).'(first_name varchar(255),last_name varchar(255),useless int(11),email_address varchar(255),voucher_code varchar(255),vendor_code varchar(255),street_address varchar(255),house_number varchar(255),zip_code varchar(255),city varchar(255),purchased date) ');
}
else
{
    echo ("file exists");
    exit;
}
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
	for ($c=0; $c < 1; $c++) {

            //only run if the first column if not equal to firstname
            if($data[0] !='firstname'){
                

				mysql_query("INSERT INTO ".$tablename."(
				first_name,
				last_name,
				useless,
				email_address,
				voucher_code,
				vendor_code,
				street_address,
				house_number,
				zip_code,
				city,
				purchased
				)VALUES(
					'".mysql_real_escape_string($data[0])."',
					'".mysql_real_escape_string($data[1])."',
					'".mysql_real_escape_string($data[2])."',
					'".mysql_real_escape_string($data[3])."',
					'".mysql_real_escape_string($data[4])."',
					'".mysql_real_escape_string($data[5])."',
					'".mysql_real_escape_string($data[6])."',
					'".mysql_real_escape_string($data[7])."',
					'".mysql_real_escape_string($data[8])."',
					'".mysql_real_escape_string($data[9])."',
					'".mysql_real_escape_string($data[10])."'
				)")or die(mysql_error());
            }

	$tot++;}
}
fclose($handle);
@$content.= "<div class='success' id='message'> CSV File Imported, $tot records added </div>";

}// end no error
}//close if isset upfile

@$er = errors($error);
@$content.= <<<EOF
<h3>Import CSV Data</h3>
$er
<form enctype="multipart/form-data" action="" method="post">
        File:<input name="uploaded" type="file" maxlength="20" /><input type="submit" name="upfile" value="Upload File">
</form>
<a type="button" href="index.php">Go to contact-us</button>
EOF;
echo $content;
?>
